﻿using VisualWip.Core.Interfaces;

namespace VisualWip.Core.Columns
{
    public abstract class WorkItemsColumnBase : Column, IWorkItemProviderColumn
    {
        private readonly IWorkItemProviderFactory _workItemProviderFactory;
        private IWorkItemSizeStratergy _sizeStratergy;
        private string _workItemProviderName;
        private WorkItemSize _workItemSize;

        protected WorkItemsColumnBase(IWorkItemProviderFactory workItemProviderFactory)
        {
            _workItemProviderFactory = workItemProviderFactory;
            _sizeStratergy = new DecresingSizeWorkItemSizeStratergy(new StaticWorkItemSizeProvider());
        }

        public string WorkItemProviderName
        {
            get { return _workItemProviderName; }
            set
            {
                if (value != _workItemProviderName)
                {
                    _workItemProviderName = value;
                    OnPropertyChanged(this, () => WorkItemProviderName);
                }
            }
        }

        public IWorkItemProvider WorkItemProvider
        {
            get { return _workItemProviderFactory.Create(WorkItemProviderName); }
        }

        public WorkItemSize WorkItemSize
        {
            get { return _workItemSize; }
            set
            {
                if (value != _workItemSize)
                {
                    _workItemSize = value;
                    OnPropertyChanged(this, () => WorkItemSize);
                }
            }
        }

        public IWorkItemSizeStratergy SizeStratergy
        {
            get { return _sizeStratergy; }
            set
            {
                if (value != _sizeStratergy)
                {
                    _sizeStratergy = value;
                    OnPropertyChanged(this, () => SizeStratergy);
                }
            }
        }

        public override PersistenceTree Persist(string name)
        {
            PersistenceTree settings = base.Persist(name);
            settings.Add(() => WorkItemProviderName);
            settings.AddChild(() => WorkItemSize);
            return settings;
        }

        public override void Load(PersistenceTree settings)
        {
            base.Load(settings);
            WorkItemProviderName = settings.Get(() => WorkItemProviderName,string.Empty);
            WorkItemSize = settings.GetChild(() => WorkItemSize);
        }
    }
}